/*
	Copyright 2009 Omer Saatcioglu
	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

		http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.

	$Author$
	$Revision$
	$LastChangedDate$
   
 */

package com.saatcioglu.android.guessthenumber;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class GfxQuickShot extends GfxGuesstheNumber {
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		SMBGuesstheNumber.nActiveDigits = SMBGuesstheNumber.nDigits;
		SMBGuesstheNumber.nActiveMaxShots = SMBGuesstheNumber.nMaxShots;
		SMBGuesstheNumber.nActiveMaxTime = SMBGuesstheNumber.nMaxTime;
		Log.d("GfxQuickShot::onCreate", Integer.toString(SMBGuesstheNumber.nActiveDigits));
		super.onCreate(savedInstanceState);
		if (SMBGuesstheNumber.isGameStarted) {
			resumetheGame();
		}
		else{
			removeDialog(R.id.display_start);
			showDialog(R.id.display_start);
		}
	}

	@Override
	protected void preparePreference() {
		SMBGuesstheNumber.gameSettings = getSharedPreferences(
				CtrlMaintanence.QUICK_SETTINGS_NAME, 0);
		Log.d("GfxQuickShot::preparePreference", "enter");
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		Log.d("GfxQuickShot::onCreateDialog", "enter");
		String message = new String();
		switch (id) {
		case R.id.display_start:
			message = formatMission()
			+ formatTip();
			return new AlertDialog.Builder(this)
			.setIcon(R.drawable.droid)
			.setTitle(R.string.menu_customshot)
			.setMessage(message)
			.setPositiveButton(R.string.alert_dialog_newgame, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,	int whichButton) {
				}
			})
			.setNegativeButton(R.string.menu_customsettings, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,	int whichButton) {
					runGameSettings();
					finish();
				}
			})
			.create();
		case R.id.display_success:
			message = getString(R.string.text_success)
			+ "\n\n"
			+ formatMission()
			+ formatTip();
			return new AlertDialog.Builder(this)
			.setIcon(R.drawable.smiley_happy)
			.setTitle(R.string.dialog_success)
			.setMessage(message)
			.setPositiveButton(R.string.alert_dialog_newgame, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,
						int whichButton) {
					StartaNewGame();
				}
			})
			.setNegativeButton(R.string.menu_customsettings, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,	int whichButton) {
					runGameSettings();
					finish();
				}
			})
			.create();
		case R.id.display_failure:
			message = getString(R.string.text_failure)
			+ "\n\n"
			+ formatMission()
			+ formatTip();
			return new AlertDialog.Builder(this)
			.setIcon(R.drawable.smiley_sad)
			.setTitle(R.string.dialog_failure)
			.setMessage(message)
			.setPositiveButton(R.string.alert_dialog_newgame, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					StartaNewGame();
				}
			})
			.setNegativeButton(R.string.menu_customsettings, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,	int whichButton) {
					runGameSettings();
					finish();
				}
			})
			.create();
		}
		return super.onCreateDialog(id);
	}
	
	private void runGameSettings(){
		startActivity(new Intent(this, GfxPreferences.class));
	}

	private String formatTip(){
		return getString(R.string.tip_gamesettings);
	}

	protected void gameSucceded() {
		super.gameSucceded();
		removeDialog(R.id.display_success);
		showDialog(R.id.display_success);
	}
}
